import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2025-07-17
 * Time: 22:21
 */
public class Test61 {
    static int[] dx = {1,-1,0,0};
    static int[] dy = {0,0,1,-1};
    static boolean[][] vis;
    static int m;
    static int n;
    static char[] word1;
    public static boolean exist(char[][] board, String word) {
        m = board.length;
        n = board[0].length;
        word1 = word.toCharArray();
        vis = new boolean[m][n];
        for(int i = 0; i < m;i++) {
            for(int j = 0; j < n; j++) {
                if(board[i][j] == word1[0]) {
                    vis[i][j] = true;
                    if(dfs(board,i,j,1)) {
                        return true;
                    }
                    vis[i][j] = false;
                }
            }
        }
        return false;
    }
    public static boolean dfs(char[][] board,int i,int j,int pos) {
        if(pos == word1.length) {
            return true;
        }
        for(int k = 0; k < 4; k++) {
            int x = i+dx[k],y=j+dy[k];
            if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == word1[pos] && !vis[x][y]) {
                vis[x][y] = true;
                if(dfs(board,x,y,pos+1)) {
                    return true;
                }
                vis[x][y] = false;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int n = sc.nextInt();
        sc.nextLine();
        char[][] box = new char[m][n];
        for (int i = 0; i < m; i++) {
            String line = sc.nextLine();
            String[] chars = line.split(" ");
            for (int j = 0; j < n; j++) {
                // 取拆分后的字符填充到二维数组
                box[i][j] = chars[j].charAt(0);
            }
        }
        String word = sc.nextLine();
        boolean ret = exist(box,word);
        System.out.println(ret);
    }
}
